package com.atguigu.gmall.item.service.imp;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/22 9:35
 * @Version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {
    //  数据汇总：service-product-client
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map getItem(Long skuId) {
        //  创建map 集合
        HashMap<String, Object> map = new HashMap<>();
        //  判断布隆过滤器中是否存在该商品
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        //  判断
        if (!bloomFilter.contains(skuId)){
            return map;
        }
        //  远程调用获取数据
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = this.productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //  获取分类数据
        CompletableFuture<Void> viewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = this.productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView",categoryView);
        },threadPoolExecutor);
        //  获取价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = this.productFeignClient.getSkuPrice(skuId);
            map.put("price",skuPrice);
        },threadPoolExecutor);

        //  获取海报信息
        CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = this.productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterList);
        },threadPoolExecutor);

        //  获取销售属性回显+锁定
        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = this.productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);
        //  获取切换数据.
        CompletableFuture<Void> skuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            //  存储Json字符串.
            String valuesSkuJson = JSON.toJSONString(idsMap);
            map.put("valuesSkuJson", valuesSkuJson);
        },threadPoolExecutor);

        //  获取规格参数--平台属性数据
        CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);
            //  attrName attrValue 放入一个对象中. 要么是map ，要么是一个实体类
            if (!CollectionUtils.isEmpty(attrList)) {
                List<HashMap<String, Object>> mapList = attrList.stream().map(baseAttrInfo -> {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("attrName", baseAttrInfo.getAttrName());
                    hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                    return hashMap;
                }).collect(Collectors.toList());
                map.put("skuAttrList", mapList);
            }
        },threadPoolExecutor);

        //  调用热度排名方法.
        CompletableFuture<Void> hotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //  多任务组合：
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                viewCompletableFuture,
                spuPosterCompletableFuture,
                spuSaleAttrCompletableFuture,
                priceCompletableFuture,
                skuJsonCompletableFuture,
                attrCompletableFuture,
                hotScoreCompletableFuture
        ).join();

        //  返回数据
        return map;
    }
}
